﻿/******************** 
	作用:会员会员等级
	作者:蔡俊雄
	版本:V1.0
	时间:2015-07-25
	
********************/

$(function() {
	var tableId = "#dynamic-table"; //表格ID
	var addUrl = "/UserCenter/MemberLevel/add"; //添加会员等级的网址
	var editUrl = "/UserCenter/MemberLevel/update"; //编辑会员等级的网址
	var deleteUrl = "/UserCenter/MemberLevel/delete"; //删除的地址

	General.extendDialog(); //扩展对话框

	//---------------------------同步会员等级
	//点击"同步会员等级"按钮
	$("#synchronous").on("click", function(e) {
		synchronousGroup();
	});
	/**
	 * 同步会员等级
	 */
	function synchronousGroup() {
		var options = {};
		var url = synchronousUrl;
		var data = {};
		var tip = "正在同步会员等级";
		Common.ajaxPost(options, beforeSynchronousGroup, url, data, tip, completeSynchronousGroup, failSynchronousGroup);
	};
	//同步会员等级前的操作
	function beforeSynchronousGroup() {
		//		General.showMask();
	};
	//同步会员等级成功
	function completeSynchronousGroup(options, data, status) {
		//		General.hideMask();
		//		data = $.parseJSON(data).data;
		//		alert(data);
		General.refresh(); //刷新页面
	};
	//同步会员等级失败
	function failSynchronousGroup() {
		//		General.hideMask();
		alert("同步会员等级失败!");
	};
	//---------------------------添加会员等级
	//点击"添加会员等级"按钮
	$("#add").on("click", function(e) {
		$("#dialog-edit").html($("#tpl-addGroup").html());
		var eventName = 'keyup blur paste';
		$("#dialog-edit .conversion").delegate(null, eventName, function(e) {
			if (e.type == "focusout" || e.type == "paste") {
				checkIsNumber($(e.target), true); //检查输入值是否为数字
				showTotalEarnings();
			} else {
				checkIsNumber($(e.target)); //检查输入值是否为数字
			}
		});
		$("#dialog-edit .order_number").delegate(null, eventName, function(e) {
			if (e.type == "focusout" || e.type == "paste") {
				checkIsNumber($(e.target), true); //检查输入值是否为数字
				showTotalEarnings();
			} else {
				checkIsNumber($(e.target)); //检查输入值是否为数字
			}
		});
		$("#dialog-edit").removeClass('hide').dialog({
			resizable: false,
			width: '360',
			modal: true,
			title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-plus green'></i> 添加会员等级</h4></div>",
			title_html: true,
			buttons: [{
				html: "<i class='ace-icon fa fa-save'></i> 确定",
				"class": "btn btn-info btn-minier btn-space-right",
				click: function() {
					var result_name = $("#dialog-edit .name").val(); //获取会员等级名称
					
					if (!result_name) {
						alert("请输入会员等级名称!");
						$("#dialog-edit .name").focus();
						return;
					}
					var result_extra_rate = $("#dialog-edit .extra_rate").val(); //获取分佣加成
					
					if (!result_extra_rate) {
						alert("请输入分佣加成!");
						$("#dialog-edit .extra_rate").focus();
						return;
					}
					var conversion = $("#dialog-edit .conversion").val();
					var order_number = $("#dialog-edit .order_number").val();
					if(!conversion && !order_number || order_number <=0 && conversion<=0) {
						alert("请输入转化人数或者订单数!");
						$("#dialog-edit .conversion").focus();
						return;

					}
					var conditional = $("#dialog-edit input[name=conditional]:checked").val();
					var options = {};
					var url = addUrl;
					var data = {
						"name": result_name,
						"extra_rate":parseFloat(result_extra_rate),
						'conversion':conversion,
						'order_number':order_number,
						'conditional':conditional,
					};
					var tip = "正在添加会员等级";
					Common.ajaxPost(options, beforeAdd, url, data, tip, completeAdd, failAdd);
				}
			}, {
				html: "<i class='ace-icon fa fa-times'></i> 取消",
				"class": "btn btn-danger btn-minier",
				click: function() {
					$(this).dialog("close");
				}
			}]
		});
	});
	//添加会员等级前的操作
	function beforeAdd() {
		General.closeEditWindow();
		General.showMask();
	};
	//添加会员等级成功
	function completeAdd(options, data, status) {
		
		data=$.parseJSON(data);		
		console.log(data);
		if(data.success){
			alert(data.sub_msg);
			General.hideMask(); 
			General.refresh(); //刷新页面
		}else if(data.error){
			if (data.msg) {
				alert(data.msg)
			} else {
				alert(data.sub_msg)
			}
			General.hideMask(); 
			
		}
		
	};
	//添加会员等级失败
	function failAdd() {
		
		alert('添加会员等级失败！');
		General.hideMask();
		
	};
	//---------------------------编辑会员等级
	//点击行内"编辑"按钮
	$(tableId + ">tbody a[data-id=edit]:not([disabled])").on("click", function(e) {
		var eventName = 'keyup blur paste';
		$("#dialog-edit").html($("#tpl-editGroup").html());
		var tr=$(this).closest("tr");
		var id = tr.attr("data-id"); //获取gid
		var text_name = tr.find("td:eq(1)").text();
		var text_extra_rate = tr.attr('data-value');
		$("#dialog-edit .name").val(text_name); //设置会员等级名称
		$("#dialog-edit .extra_rate").val(text_extra_rate); //设置会员等级名称
		$("#dialog-edit .conversion").val($(this).attr('data-conversion')); //设置转化人数
		$("#dialog-edit .order_number").val($(this).attr('data-order')); //设置订单数
		var con = $(this).attr('data-conditional');
		$("#dialog-edit input[name=conditional]").each(function(i, item) {
			if($(item).val() === con) {
				$(item).attr("checked", "checked");
			}
		}); //设置订单数

		$("#dialog-edit .conversion").delegate(null, eventName, function(e) {
			if (e.type == "focusout" || e.type == "paste") {
				checkIsNumber($(e.target), true); //检查输入值是否为数字
				showTotalEarnings();
			} else {
				checkIsNumber($(e.target)); //检查输入值是否为数字
			}
		});
		$("#dialog-edit .order_number").delegate(null, eventName, function(e) {
			if (e.type == "focusout" || e.type == "paste") {
				checkIsNumber($(e.target), true); //检查输入值是否为数字
				showTotalEarnings();
			} else {
				checkIsNumber($(e.target)); //检查输入值是否为数字
			}
		});
		$("#dialog-edit").removeClass('hide').dialog({
			resizable: false,
			width: '360',
			modal: true,
			title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-edit green'></i> 编辑会员等级</h4></div>",
			title_html: true,
			buttons: [{
				html: "<i class='ace-icon fa fa-save'></i> 确定",
				"class": "btn btn-info btn-minier btn-space-right",
				click: function() {
					
					var result_name = $("#dialog-edit .name").val(); //获取会员等级名称
					
					if (!result_name) {
						alert("请输入会员等级名称!");
						$("#dialog-edit .name").focus();
						return;
					}
					var result_extra_rate = $("#dialog-edit .extra_rate").val(); //获取分佣加成
					
					if (!result_extra_rate) {
						alert("请输入分佣加成!");
						$("#dialog-edit .extra_rate").focus();
						return;
					}
					var conversion = $("#dialog-edit .conversion").val();
					var order_number = $("#dialog-edit .order_number").val()
					if(!conversion && !order_number || order_number <=0 && conversion<=0) {
						alert("请输入转化人数或者订单数!");
						$("#dialog-edit .conversion").focus();
						return;

					}
					var conditional = $("#dialog-edit input[name=conditional]:checked").val();
					var options = {};
					var url = editUrl;
					var data = {
						"id": id,
						"name": result_name,
						"extra_rate":parseFloat(result_extra_rate),
						'conversion':conversion,
						'order_number':order_number,
						'conditional':conditional,
					};
					var tip = "正在编辑会员等级";
					
					Common.ajaxPost(options, beforeEdit, url, data, tip, completeEdit, failEdit);
				}
			}, {
				html: "<i class='ace-icon fa fa-times'></i> 取消",
				"class": "btn btn-danger btn-minier",
				click: function() {
					$(this).dialog("close");
				}
			}]
		});
	});
	//编辑会员等级前的操作
	function beforeEdit() {
		General.closeEditWindow();
		General.showMask();
	};
	//编辑会员等级成功
	function completeEdit(options, data, status) {
		
		data=$.parseJSON(data);		
		console.log(data);
		if(data.success){
			alert(data.sub_msg);
			General.hideMask(); 
			General.refresh(); //刷新页面
		}else if(data.error){	
			if (data.msg) {
				alert(data.msg)
			} else {
				alert(data.sub_msg)
			}
			General.hideMask(); 
			
		}
	};
	//编辑会员等级失败
	function failEdit() {
		
		alert("编辑会员等级失败!");
		General.hideMask();
		
	};
	//-----------------删除-----------------
	//点击删除所选按钮
	/*$("#delete").on("click", function(e) {
		General.stopEvent(e);
		//判断是否有行被选中
		var selectedRows = $(tableId+' > tbody input[type=checkbox]:checked');
		if (selectedRows.length > 0) {
			//弹出确认框
			if (confirm("您确定要删除所有选中的会员等级吗?")) {
				var ids = [];
				var id;
				selectedRows.each(function(index, checkbox) {
					id = $(this).closest("tr").attr("data-id");
					ids.push(id);
				});
				startDelete(ids); //开始删除
			}
		} else {
			General.alert("请先选中要删除的会员等级再进行操作!");
		}
	});*/
	//点击行内"删除"按钮
	$(tableId + ">tbody a[data-id=delete]:not([disabled])").on("click", function(e) {
		var tr = $(this).closest("tr");
		var id = tr.attr("data-id");
		var text = tr.find("td:eq(1)").text();
		if (confirm("您确定要删除会员等级[" + text + "]吗?")) {
			//		if (confirm("您确定要删除该会员等级吗?")) {
			startDelete([id]); //开始删除消息
		}
	});
	//开始删除
	function startDelete(ids) {
		General.showMask();
		var options = {
			"id": ids
		};
		var url = deleteUrl;
		var data = {
			"id": ids.join(",")
		};
		var tip = "正在删除";
		Common.ajaxPost(options, null, url, data, tip, completeDelete, failDelete);
	};
	//删除成功
	function completeDelete(options, data, status) {
		
		data=$.parseJSON(data);		
		console.log(data);
		if(data.success){
			alert(data.sub_msg);
			General.hideMask(); 
			window.location.href='/UserCenter/MemberLevel';
		}else if(data.error){	
			if (data.msg) {
				alert(data.msg)
			} else {
				alert(data.sub_msg)
			}
			General.hideMask(); 
			
		}
	};
	//删除失败
	function failDelete() {
		alert("删除失败!");
		General.hideMask();
		
	};
	/**
	 * 检查输入值是否为数字
	 * @param {String} element 目标元素
	 */
	function checkIsNumber(element, checkValid) {
		var value = element.val().replace(/[^.0123456789]/g, '');
		if (checkValid) {
			var re = /^[1-9]\d*|^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$/; //非负整数+小数点  							
			if (!re.test(value) && value != "") {
				$(element).val(0);
			} else {
				element.val(value);
			}
		} else {
			element.val(value);
		}

	};
});
